package com.nvshen.zhangmeiying.utils;

import android.content.Context;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nvshen.zhangmeiying.Constants;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class NetUtils
{
    private final static int DEFAULT_TIMEOUT = 60000;
    // 上次请求的地址
    public static String lastURL = null;
    public static RequestParams lastParams = null;

    // 使用async——http做get请求
    public static void get(String url, RequestParams params,
                           AsyncHttpResponseHandler responseHandler)
    {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(DEFAULT_TIMEOUT);
        client.get(url, params, responseHandler);
    }

    // 使用async——http做get请求
    public static void get(String url,
                           AsyncHttpResponseHandler responseHandler)
    {
        //公共数据
        if (url.contains("?"))
        {
            url += "&device=android&version=" + Constants.VERISON_NAME + "&nvshen_uid=" + Constants.currentUser.getId();
        }
        else
        {
            url += "?device=android&version=" + Constants.VERISON_NAME + "&nvshen_uid=" + Constants.currentUser.getId();
        }
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(DEFAULT_TIMEOUT);
        client.get(url, responseHandler);
    }

    public static void post(Context context, String url, RequestParams params,
                            AsyncHttpResponseHandler responseHandler)
    {
        if (Utils.isConnectingToInternet(context))
        {
            AsyncHttpClient client = new AsyncHttpClient();
            client.setTimeout(DEFAULT_TIMEOUT);
            client.post(url, params, responseHandler);
        }
        else
        {
            responseHandler.onFailure(-101, null, new String("网络连接失败").getBytes(), null);
        }
    }

    public static void postJSON(Context context, String url, JSONObject obj, AsyncHttpResponseHandler responseHandler)
    {
        if (Utils.isConnectingToInternet(context))
        {
            try
            {
                obj.put("device", "android");
                obj.put("version", Constants.VERISON_NAME);
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
            String body = obj.toString();
            AsyncHttpClient client = new AsyncHttpClient();
            client.setTimeout(DEFAULT_TIMEOUT);
            ByteArrayEntity entity = null;
            try
            {
                byte[] bytes = body.getBytes("utf-8");
                entity = new ByteArrayEntity(bytes);
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }

            client.post(context, url, entity, "application/json",
                    responseHandler);
        }
        else
        {
            responseHandler.onFailure(-101, null, new String("网络连接失败").getBytes(), null);
        }
    }

}
